package base_eth

import (
	"context"
	"fmt"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
	"math/big"
)

func Eth() {
	fmt.Println("hello world")
	client, err := ethclient.Dial("http://8.153.91.50:8545")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// 账户地址
	address := common.HexToAddress("0xa034a80f004e0561a2e37cbcb1cde9ed06e4d403")

	balance, err := client.BalanceAt(context.Background(), address, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("余额:" + balance.String())
	fBalance := new(big.Float)
	fBalance.SetString(balance.String())
	ethValue := new(big.Float).Quo(fBalance, big.NewFloat(1e18))
	fmt.Printf("账户余额（ETH）: %f\n", ethValue)
	fmt.Println("==============================================================")

	transaction := common.HexToHash("0x58ead67496762e0b17d421b14944925aa2436ef618acb89be7ae43e5647a2340")

	tx, pending, err := client.TransactionByHash(context.Background(), transaction)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(tx.Hash().Hex())
	fmt.Println(tx.Value().String())
	fmt.Println(string(tx.Data()))
	fmt.Println(pending)
}
